package com.lianniu.web.util;


import javax.imageio.ImageIO;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 * Created by Administrator on 2017-06-24.
 */
public class ImageCompressUtil {
    /**
     * 程序入口
     * 将图片按照指定的图片尺寸压缩
     * @param outImgPath
     *            输出的压缩图片的路径
     * @param new_w
     *            压缩后的图片宽
     * @param new_h
     *            压缩后的图片高
     */
    public static byte[] compressImage(InputStream inputStream, String outImgPath, int new_w, int new_h) {
        try {
            BufferedImage src = InputImage(inputStream);
            return disposeImage(src, outImgPath, new_w, new_h);
        } catch (Exception e) {
            return null;
        }
    }
    /**
     * 读取图片
     */
    private static BufferedImage InputImage(InputStream inputStream) {
        BufferedImage srcImage = null;
        try {
            srcImage = ImageIO.read(inputStream);
        } catch (IOException e) {
            System.out.println("读取图片文件出错！" + e.getMessage());
            e.printStackTrace();
        }
        return srcImage;
    }
    /**
     * 处理图片
     * @param src
     * @param outImgPath
     * @param new_w
     * @param new_h
     */
    private static byte[] disposeImage(BufferedImage src, String outImgPath, int new_w, int new_h) {
        // 得到图片
        int old_w = src.getWidth();
        // 得到源图宽
        int old_h = src.getHeight();
        // 得到源图长
        BufferedImage newImg = null;
        // 判断输入图片的类型
        switch (src.getType()) {
            case 13:
                // png,gifnewImg = new BufferedImage(new_w, new_h,
                // BufferedImage.TYPE_4BYTE_ABGR);
                return null;
            default:
                newImg = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_RGB);
                break;
        }
        Graphics2D g = newImg.createGraphics();
        // 从原图上取颜色绘制新图
        g.drawImage(src, 0, 0, old_w, old_h, null);
        g.dispose();
        // 根据图片尺寸压缩比得到新图的尺寸
        newImg.getGraphics().drawImage(src.getScaledInstance(new_w, new_h, Image.SCALE_SMOOTH), 0, 0, null);

        // 调用方法输出图片文件
        return OutImage(outImgPath, newImg);
    }

    /**
     * 将图片文件输出到指定的路径，并可设定压缩质量
     *
     * @param outImgPath
     * @param newImg
     * @param
     */
    private static byte[] OutImage(String outImgPath, BufferedImage newImg) {
        // 判断输出的文件夹路径是否存在，不存在则创建
        outImgPath = "D:\\data\\aaa.png";
        File file = new File(outImgPath);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        } // 输出到文件流
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(byteArrayOutputStream);
        try {
            ImageIO.write(newImg, outImgPath.substring(outImgPath.lastIndexOf(".") + 1), bufferedOutputStream);
//            ImageIO.write(newImg, outImgPath.substring(outImgPath.lastIndexOf(".") + 1), file);
            byte[] bytes = byteArrayOutputStream.toByteArray();
            return bytes;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                byteArrayOutputStream.close();
                bufferedOutputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }

    public static void main(String[] args) throws FileNotFoundException {
        FileInputStream fileInputStream = new FileInputStream(new File("D:\\aaa.png"));

        ImageCompressUtil.compressImage(fileInputStream, "", 136, 76);
    }
}
